package com.atguigu.order.controller;


import com.atguigu.commonutils.R;
import com.atguigu.order.service.TPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-18
 */
@RestController
@RequestMapping("/order/tpaylog")
//@CrossOrigin
public class TPayLogController {

    @Autowired
    TPayLogService tPayLogService;

    //生成微信支付二维码
    @GetMapping("/createWxZfEwm/{order_no}")
    public R createWxZfEwm(@PathVariable String order_no){
        Map map=tPayLogService.createWxZfEwm(order_no);
        return R.ok().data(map);
    }

    //查询订单支付状态
    @GetMapping("queryPayStatus/{order_no}")
    public R queryPayStatus(@PathVariable String order_no){
        //查询订单支付状态
        Map<String,String> map = tPayLogService.queryPayStatus(order_no);
        if(map ==  null){
            return R.error().message("支付出错了");
        }
        //如果返回的map不为空，通过map获取订单状态
        if(map.get("trade_state").equals("SUCCESS")){
            //添加记录到支付表，更新订单表订单状态
            tPayLogService.updateOrderStatus(map);
            return R.ok();
        }
        return R.ok().message("支付中");
    }


}

